﻿Namespace myBookmarks.Web
    Public Module PagingExtensions

#Region "HtmlHelper extensions"
        <System.Runtime.CompilerServices.Extension()> _
        Public Function Pager(ByVal htmlHelper As HtmlHelper, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer) As String
            Return Pager(htmlHelper, pageSize, currentPage, totalItemCount, Nothing, Nothing)
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function Pager(ByVal htmlHelper As HtmlHelper, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer, ByVal actionName As String) As String
            Return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, Nothing)
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function Pager(ByVal htmlHelper As HtmlHelper, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer, ByVal values As Object) As String
            Return Pager(htmlHelper, pageSize, currentPage, totalItemCount, Nothing, New RouteValueDictionary(values))
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function Pager(ByVal htmlHelper As HtmlHelper, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer, ByVal actionName As String, ByVal values As Object) As String
            Return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, New RouteValueDictionary(values))
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function Pager(ByVal htmlHelper As HtmlHelper, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer, ByVal valuesDictionary As RouteValueDictionary) As String
            Return Pager(htmlHelper, pageSize, currentPage, totalItemCount, Nothing, valuesDictionary)
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function Pager(ByVal htmlHelper As HtmlHelper, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer, ByVal actionName As String, ByVal valuesDictionary As RouteValueDictionary) As String
            If valuesDictionary Is Nothing Then
                valuesDictionary = New RouteValueDictionary()
            End If
            If actionName IsNot Nothing Then
                If valuesDictionary.ContainsKey("action") Then
                    Throw New ArgumentException("The valuesDictionary already contains an action.", "actionName")
                End If
                valuesDictionary.Add("action", actionName)
            End If
            Dim _pager = New Pager(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary)
            Return _pager.RenderHtml()
        End Function

#End Region


#Region "IEnumerable extensions"

        <System.Runtime.CompilerServices.Extension()> _
        Public Function ToPagedList(Of T)(ByVal source As IEnumerable(Of T), ByVal pageIndex As Integer, ByVal pageSize As Integer) As IPagedList(Of T)
            Return New PagedList(Of T)(source, pageIndex, pageSize)
        End Function

        <System.Runtime.CompilerServices.Extension()> _
        Public Function ToPagedList(Of T)(ByVal source As IEnumerable(Of T), ByVal pageIndex As Integer, ByVal pageSize As Integer, ByVal totalCount As Integer) As IPagedList(Of T)
            Return New PagedList(Of T)(source, pageIndex, pageSize, totalCount)
        End Function

#End Region
    End Module
End Namespace
